//@author A0106044W
package logic;

import storage.Date;
import storage.Time;

/**
 * 
 * 
 * 	       Sothearith Sreang
 *         All of the Logic part is written by me.
 * 
 */

public class Helper {
	
	private final static int MAXIMUM_YEAR = 2999;
	private final static int MAXIMUM_HOUR = 23;
	private final static int MAXIMUM_MINUTE = 59;
	private final static int MAXIMUM_SECOND = 59;
	
	private final static String EMPTY_STRING = "";
	private final static String COMPLETE_STATEMENT = "Complete";
	private final static String PENDING_STATEMENT = "Pending";
	
	private final static int NOON_HOUR = 12;
	private static final int MIDNIGHT_HOUR = 0;
	private static final int TWELVE_HOURS = 12;
	private final static int TWO_DIGIT_NUMBER_TEN = 10;
	
	private final static boolean IS_OVERDUE = true;
	private final static boolean IS_COMPLETE = true;
	
	
	private static int EQUAL = 0;
	
	private static final String[] MONTH = { "January", "February", "March",
		"April", "May", "June", "July", "August", "September", "October",
		"November", "December" };
	
	
	public static String setUpFeedback(String templateFeedback, int size) {
		String plural = " is";

		if (size > 1) {
			plural = "s are";
		} 

		String feedback = String.format(templateFeedback, size, plural);
		return feedback;
	}
	
	 public static String getOverDueForm(boolean overDueP) {
		String overDueForm = EMPTY_STRING;
		
		if(overDueP == IS_OVERDUE) {
			overDueForm = "Over due";
		} else {
			overDueForm = "Not over due";
		}
		
		return overDueForm;
	}

	public static String getDateForm(Date date) {
		String dateForm = EMPTY_STRING;
		Date maximumDate = new Date(MAXIMUM_YEAR);
		
		
		if (date == null) {
			dateForm = EMPTY_STRING;
		} else if (date.compareTo(maximumDate) == EQUAL) {
			dateForm = EMPTY_STRING;
		}else {

			String dayForm = EMPTY_STRING;
			String monthForm = EMPTY_STRING;
			String yearForm = EMPTY_STRING;

			if (date.getDay() != null) {
				int day = date.getDay();
				dayForm = getDayForm(day);
			}

			if (date.getMonth() != null) {
				int month = date.getMonth();
				monthForm = getMonthForm(month);
			}

			if (date.getYear() != null) {
				int year = date.getYear();
				yearForm = getYearForm(year);
			}

			if (dayForm == EMPTY_STRING) {
				if (monthForm == EMPTY_STRING) {
					if (yearForm == EMPTY_STRING) {
						dateForm = EMPTY_STRING;
					} else {
						dateForm = yearForm;
					}
				} else {
					dateForm = monthForm + " " + yearForm;
				}
			} else {
				dateForm = dayForm + " " + monthForm + " " + yearForm;
			}

		}

		return dateForm;
	}

	
	public static String getTimeForm(Time time) {
		String timeForm = EMPTY_STRING;
		Time maximumTime = null;
		
		try{
			maximumTime = new Time(MAXIMUM_HOUR, MAXIMUM_MINUTE, MAXIMUM_SECOND);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (time == null) {
			timeForm = EMPTY_STRING;
		} else if(time.compareTo(maximumTime) == EQUAL) {
			timeForm = EMPTY_STRING;
		} else {
			
			String hourForm = EMPTY_STRING;
			String minuteForm = EMPTY_STRING;
			String specifier = EMPTY_STRING;

			if (time.getHour() != null) {
				int hour = time.getHour();
				hourForm = getHourForm(hour);
				specifier = getSpeciferForm(hour);
			}

			if (time.getMinute() != null) {
				int minute = time.getMinute();
				minuteForm = getMinuteForm(minute);
			}
			// int second = time.getSecond();

			if (minuteForm == EMPTY_STRING) {
				if (hourForm == EMPTY_STRING) {
					timeForm = EMPTY_STRING;
				} else {
					timeForm = hourForm + " " + specifier;
				}
			} else {
				timeForm = hourForm + ":" + minuteForm + " " + specifier;
			}
		} 

		return timeForm;
	}

	public static String getStatusForm(boolean status) {
		String statusForm = EMPTY_STRING;

		if (status == IS_COMPLETE) {
			statusForm = COMPLETE_STATEMENT;
		} else {
			statusForm = PENDING_STATEMENT;
		}

		return statusForm;
	}

	// next level abstraction
	
	private static String getMonthForm(int month) {
		return MONTH[month - 1];
	}

	private static String getYearForm(int year) {
		return String.format("%1d", year);
	}

	private static String getDayForm(int day) {
		String dayForm = EMPTY_STRING;

		if (day < TWO_DIGIT_NUMBER_TEN) {
			dayForm = String.format(" %1d", day);
		} else {
			dayForm = String.format("%1d", day);
		}

		return dayForm;
	}

	private static String getSpeciferForm(int hour) {
		String specifier = EMPTY_STRING;

		if (hour < NOON_HOUR) {
			specifier = "AM";
		} else {
			specifier = "PM";
		}

		return specifier;
	}

	private static String getMinuteForm(int minute) {
		String minuteForm = EMPTY_STRING;

		if (minute < TWO_DIGIT_NUMBER_TEN) {
			minuteForm = String.format("0%1d", minute);
		} else {
			minuteForm = String.format("%1d", minute);
		}

		return minuteForm;
	}

	private static String getHourForm(int hour) {
		String hourForm = EMPTY_STRING;

		if (hour < TWO_DIGIT_NUMBER_TEN) {
			if(hour > MIDNIGHT_HOUR) {
				hourForm = String.format(" %1d", hour);
			} else {
				hourForm = String.format("%1d", hour + TWELVE_HOURS);
			}
		} else {
			if(hour > NOON_HOUR) {
				hourForm = String.format("%1d", hour - NOON_HOUR);
			} else {
				hourForm = String.format("%1d", hour);
			}
		}

		return hourForm;
	}
	
}
